package com.valveinspectionsys.maintain.activity;

import java.util.ArrayList;
import java.util.List;

import com.valveinspectionsys.R;
import com.valveinspectionsys.dbhelp.XmlNameHelper;
import com.valveinspectionsys.gridview.GridMainActivity;
import com.valveinspectionsys.gridview.QueryRoutePlanMap;
import com.valveinspectionsys.maintain.db.DeviceMaintainDao;
import com.valveinspectionsys.povo.DeviceVo;
import com.valveinspectionsys.routing.activity.DeviceInfoActivity;
import com.valveinspectionsys.util.SharedPreferenceUtil;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 保养设备信息
 * @author King
 *
 */
public class MaintainDeviceInfoActivity extends Activity{
    public static final int ITEM0 = Menu.FIRST;// 系统值
    public static final int REQUEST_CODE_BANDING=1;//绑定
    public static final int REQUEST_CODE_NORFID=2;//异常巡检
    public TextView textview1;//设备名称
    public TextView textview2;//设备编号
    public TextView textview3;//设备类型
    public TextView textview4;//设备地址
    public TextView textview5;//公称通径
    public TextView textview6;//公称压力
    public TextView textview7;//生产商
    public TextView textview8;//安装日期
    public TextView textview9;//设备状态
    public TextView textview10;//巡检项目
    public Button   button;
    
    public DeviceMaintainDao  dao;
    
    public SharedPreferenceUtil  sh;
    
    public MaintainQueryActivityGroup  act;
    
    public DeviceVo device;
    
    public List<String> result=new ArrayList<String>();//设备巡检结果显示
    public String[] results=null;
    
    
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
	
	super.onCreate(savedInstanceState);
	setContentView(R.layout.device_info);
	act=(MaintainQueryActivityGroup)getParent();
	sh=new SharedPreferenceUtil(this);
	dao=new DeviceMaintainDao(this, sh.getValue(XmlNameHelper.SqliteName));
	init();
    }

    //初始化可视化控件
    public void init(){
	textview1=(TextView)findViewById(R.id.device_info_devicename);
	textview2=(TextView)findViewById(R.id.device_info_devicenumber);
	textview3=(TextView)findViewById(R.id.device_info_devicetype);
	textview4=(TextView)findViewById(R.id.device_info_deviceadds);
	textview5=(TextView)findViewById(R.id.device_info_deviceCaliber);
	textview6=(TextView)findViewById(R.id.device_info_devicePressure);
	textview7=(TextView)findViewById(R.id.device_info_producers);
	textview8=(TextView)findViewById(R.id.device_info_InstallationDate);
	textview9=(TextView)findViewById(R.id.device_info_copystatus);
	textview10=(TextView)findViewById(R.id.device_info_deviceitem);
	TextView t=(TextView)findViewById(R.id.device_info_textView10);
	t.setText("保养状态");
	button=(Button)findViewById(R.id.device_info_result);
	button.setText("点击显示保养结果");
	button.setOnClickListener(new OnClickListener() {
	    
	    @Override
	    public void onClick(View v) {
		
		showDialog();
	    }
	});
    }
    
    //初始化数据
   public void initDate(){
	  device=dao.queryRfidDevice(null, act.getDeviceid(),act.getInfolistid());
	  if(device==null){
	      return;
	  }
	textview1.setText(device.getOpenStatus());
	textview2.setText(device.getDeviceNumber());
	textview3.setText(device.getDeviceType());
	textview4.setText(device.getDeviceAdds());
	textview4.setSelected(true);
	textview5.setText(device.getDeviceCaliber());
	textview6.setText(device.getDevicePressure());
	textview7.setText(device.getProducers());
	textview8.setText(device.getInstallationDate());
	textview9.setText(device.getCopystatus());
	textview10.setText(device.getUseStatus());
    }
   
   @Override
   public boolean onCreateOptionsMenu(Menu menu) {

	
	menu.add(0, ITEM0 + 1, 0, "百度地图");
	menu.add(0, ITEM0 + 2, 0, "图文绑定");
	menu.add(0, ITEM0 + 3, 0, "设备绑定");
	menu.add(0, ITEM0 + 4, 0, "RFID异常保养");

	menu.findItem(ITEM0 + 1);
	// 获取MenuInflater对象
	// setMenuBackGround();
	return true;

   }

   @Override
   public boolean onOptionsItemSelected(MenuItem item) {
	if (device == null) {
	    Toast.makeText(MaintainDeviceInfoActivity.this, "没有设备不能进行些操作", Toast.LENGTH_LONG).show();
	    return false;
	}
	Intent intent=null;
	if (item.getItemId() == ITEM0) {
	    
	} else if (item.getItemId() == (ITEM0 + 1)) {
	    if(device.getLatitude()<=0){
		Toast.makeText(MaintainDeviceInfoActivity.this, "当前设备没有地理信息", Toast.LENGTH_SHORT).show();
		return false;
	    }
	    intent=new Intent(MaintainDeviceInfoActivity.this,QueryRoutePlanMap.class );
	    intent.putExtra("latitude", device.getLatitude());
	    intent.putExtra("longitude", device.getLongitude());
	    intent.putExtra("devicelistid", device.getDeviceListId());
	    startActivity(intent);
	} else if (item.getItemId() == (ITEM0 + 2)) {
	    intent=new Intent(MaintainDeviceInfoActivity.this, GridMainActivity.class);
	    intent.putExtra("deviceid", device.getDeviceId());
	    intent.putExtra("devicelistid", device.getDeviceListId());
	    intent.putExtra("type", "2");
	    startActivityForResult(intent, REQUEST_CODE_BANDING);
	} else if (item.getItemId() == (ITEM0 + 3)) {
	    intent=new Intent(MaintainDeviceInfoActivity.this, MaintainBandingActivity.class);
	    intent.putExtra("deviceid", device.getDeviceId());
	    startActivityForResult(intent, REQUEST_CODE_BANDING);
	} else if (item.getItemId() == (ITEM0 + 4)) {
	    intent=new Intent(MaintainDeviceInfoActivity.this, MaintainDeviceNoRfid.class);
	    intent.putExtra("deviceid", device.getDeviceId());
	    intent.putExtra("devicelistid", act.getInfolistid());
	    act.startActivityForResult(intent, REQUEST_CODE_NORFID);
	} 
	return super.onOptionsItemSelected(item);
   }

   public void handleActivityResult(int requestCode, int resultCode, Intent data) {
              if(requestCode==REQUEST_CODE_NORFID){
        	  if(resultCode==RESULT_OK){
        	      initDate();
        	  }
              }
   }
   
   
   //显示详细信息
   public void showDialog(){
         result=dao.queryResult(device);
	results=new String[result.size()];
	for(int i=0;i<result.size();i++){
	    results[i]=result.get(i);
	}
	AlertDialog.Builder dialog=new AlertDialog.Builder(this);
	dialog.setTitle("保养结果信息");
	
	dialog.setItems(results, null);
	
	dialog.setNegativeButton("确定", new DialogInterface.OnClickListener() {
	    
	    @Override
	    public void onClick(DialogInterface dialog, int which) {
		dialog.dismiss();
		
	    }
	});
	
    dialog.create();
    dialog.show();
   }
   
    @Override
    protected void onDestroy() {
	dao.close();
	super.onDestroy();
    }
    
  
    
}
